package com.shop.loan.handler;

import com.shop.loan.mapper.LoanOrderAuthByMybatisMapper;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.mapper.LoanOrderSignLogByMybatisMapper;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.LoanOrderAuthByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.model.enums.OrderStatus;
import com.shop.loan.model.enums.SignStatus;
import com.shop.loan.model.view.LoanOrderByLoan;
import com.shop.loan.model.vo.LoanOrderByLoanAuthVo;
import com.shop.loan.service.AsyncService;
import com.shop.loan.util.LoanOrderUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.upms.service.EruptUserService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 放款列表审核操作
 */
@Service
public class LoanOrderByLoanAuthHandler implements OperationHandler<LoanOrderByLoan, LoanOrderByLoanAuthVo> {

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    LoanOrderAuthByMybatisMapper loanOrderAuthByMybatisMapper;

    @Resource
    EruptUserService eruptUserService;

    @Lazy
    @Resource
    LoanOrderUtil loanOrderUtil;

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    LoanOrderSignLogByMybatisMapper loanOrderSignLogByMybatisMapper;

    @Resource
    AsyncService asyncService;

    @Override
    public String exec(List<LoanOrderByLoan> data, LoanOrderByLoanAuthVo loanOrderByLoanAuthVo, String[] param) {
        for (LoanOrderByLoan datum : data) {
            LoanOrderByMybatis loanOrderByMybatis = new LoanOrderByMybatis();
            loanOrderByMybatis.setId(datum.getId());

            LoanOrderByMybatis dbOrder = loanOrderByMybatisMapper.selectByPrimaryKey(datum.getId());
            if (dbOrder == null || dbOrder.getStatus() != OrderStatus.wait_loan.getStatus().intValue()) {
                return "msg.error('" + Code.ORDER_STATUS_ERROR.getMsg() + "')";
            }
            if (loanOrderByLoanAuthVo.getType() == 1) {
                /**
                 * 放款操作
                 */
                ResResult loanRes = loanOrderUtil.loan(datum.getId(), eruptUserService.getCurrentEruptUser().getName());
                if (loanRes.getCode().intValue() == Code.SUCCESS.getCode()) {
                    loanOrderByMybatis.setLoanTime(new Date());
                } else {
                    return "msg.error('" + loanRes.getMsg() + "')";
                }
            } else {
                loanOrderByMybatis.setStatus(OrderStatus.refuse_loan.getStatus());
                loanOrderByMybatis.setSignStatus(SignStatus.wait_create.getType());
                if (loanOrderByLoanAuthVo.getUserStatus() != null) {
                    UserByMybatis updateUser = new UserByMybatis();
                    updateUser.setId(datum.getUserId());
                    updateUser.setMachineStatus(loanOrderByLoanAuthVo.getUserStatus());
                    userByMybatisMapper.updateByPrimaryKeySelective(updateUser);
                }
                /**
                 * 删除合同
                 */
                loanOrderSignLogByMybatisMapper.deleteByOrderId(loanOrderByMybatis.getId());
            }
            loanOrderByMybatisMapper.updateByPrimaryKeySelective(loanOrderByMybatis);

            //插入一条审核记录
            LoanOrderAuthByMybatis loanOrderAuthByMybatis = new LoanOrderAuthByMybatis();
            loanOrderAuthByMybatis.setAuthBy(eruptUserService.getCurrentEruptUser().getName());
            loanOrderAuthByMybatis.setOrderId(datum.getId());
            loanOrderAuthByMybatis.setAuthStatus(loanOrderByLoanAuthVo.getType() == 1);
            if (loanOrderByLoanAuthVo.getType() != 1) {
                loanOrderAuthByMybatis.setOrderStatus(OrderStatus.refuse_loan.getStatus());
            }
            loanOrderAuthByMybatis.setCreateTime(new Date());
            loanOrderAuthByMybatis.setReason(loanOrderByLoanAuthVo.getReason());
            loanOrderAuthByMybatisMapper.insertSelective(loanOrderAuthByMybatis);

            if (loanOrderByLoanAuthVo.getType() != 1) {
                asyncService.notifyCheck(loanOrderByMybatis.getId());
            }
        }
        return null;
    }
}
